﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Diagnostics;

namespace CAD_Project_1
{
    class ParseTxt
    {
        public StreamReader sr;
        public StreamWriter sw;
        public StreamWriter swInsert;
        public ParseTxt(StreamReader i, string filenameOut) {            
            this.sr = i;            
            sw = new StreamWriter(filenameOut+"Update.txt");
            swInsert = new StreamWriter(filenameOut+"Insert.txt");
        }

        public void parseFile() {            
            string line;
            Boolean novo = false;
            Boolean host = false;
            Stopwatch stop = new Stopwatch();
            stop.Start();            
            while ((line = sr.ReadLine())!=null ) {                           
                if (line.Contains("---") && !host) {
                    novo = true;
                    host = false;
                }
                if (line.Contains("<host>") && novo)
                {
                    host = true;
                }
                if (line.Contains("***") && novo)
                {
                    novo = false;
                    host = false;
                }                
                if (line.Contains("+ ")) {
                    novo = true;
                    host = true;
                }                

                if (novo && host)
                {
                    if (line.Contains('!') || line.Contains("<id>"))
                    {
                        line = line.Replace('!', ' ');
                        
                        sw.WriteLine(line);
                    }
                    if (line.StartsWith("+  "))
                    {
                        line = line.Replace('+', ' ');
                        swInsert.WriteLine(line);
                    }
                    if (line.Contains("</host>"))
                    {
                        novo = false;
                        host = false;
                    }
                }                                                                   
            }
            sw.WriteLine("</hosts>");
            sw.Flush();
            sw.Dispose();
            sw.Close();
            sr.Dispose();
            sr.Close();
            stop.Stop();
            Console.WriteLine(stop.ElapsedMilliseconds);
        }        
    }
}
